package com.joymeter.common.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName TimeTools
 * @Description 时间工具类
 * @Author liang
 * @Date 2018/6/1 13:59
 * @Version 1.0
 **/
public class TimeTools {

    /** * 获得指定日期的前一天 *

     @param specifiedDay

      * @return

     * @throws Exception */

    public static String getSpecifiedDayBefore(String specifiedDay){

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Calendar c = Calendar.getInstance();

        Date date=null;

        try {

            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);

        } catch (ParseException e) {

            e.printStackTrace();

        }

        c.setTime(date); int day=c.get(Calendar.DATE);

        c.set(Calendar.DATE,day-1);

        String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());

        return dayBefore;

    }


    /**
     * 获得指定日期的后一天
     * @param specifiedDay
     * @return
     */
    public static String getSpecifiedDayAfter(String specifiedDay){
        Calendar c = Calendar.getInstance();
        Date date=null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day=c.get(Calendar.DATE);
        c.set(Calendar.DATE,day+1);

        String dayAfter=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return dayAfter;
    }


    /**
     * 时间戳( 转 小时
     *
     * @param dateTime
     * @return
     */
    public static int timestampToHour(String dateTime){
        SimpleDateFormat hourFormat=new SimpleDateFormat("HH");
        int hour =  Integer.valueOf(hourFormat.format(new Date(Long.valueOf(dateTime))));
        return hour;
    }

    public static String timestampToDate(String dataTime){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String format = dateFormat.format(Long.valueOf(dataTime));
        return format;
    }
    public static String getCurrentDate(Date date){
        Timestamp currentDate =Timestamp.valueOf( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS ").format(date));
        return currentDate.toString();

    }

    public static int timeLag(String current , String updateTime) throws ParseException {
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");//如2016-08-10 20:40
        long from = simpleFormat.parse(current).getTime();
        long to = simpleFormat.parse(updateTime).getTime();
        int minutes = (int) ((from - to)/(1000 * 60));
        return minutes;
    }
}
